package com.happiness.gateway.config.exception;

import cn.hutool.json.JSONUtil;
import com.happiness.base.http.ResultInfo;
import com.happiness.base.utils.exception.FeignErrorException;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.*;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;

/**
 * feign统一返回数据处理
 * */
@Configuration
public class GlobalFeignFallBack implements ErrorDecoder {

    @Override
    public Exception decode(String s, Response response) {
        try {
            String result = Util.toString(response.body().asReader(Charset.defaultCharset()));
            ResultInfo resultInfo = JSONUtil.toBean(result, ResultInfo.class);
            return new FeignErrorException(resultInfo.getCode(),resultInfo.getMessage());
        }catch (Exception e){
        }
        return null;
    }

}
